#encoding:utf-8 

import os, sys, string
import logging

TEMP_LATE = '''--!!The script is for gameLogic--is a node ||*in the mid of scene* || *exits multi* || *sort by zorder*!!-- \n\
--					         The View named {viewname} crteated by py-tool!\n\
-----------------------------------------------------------------------------------------------------------\n\n\
local {viewname} = class("{viewname}" , require({parentname})) \n\
\n\
--===========================================================================\n\
--default params for show\n\n\
--===========================================================================\n\
\n\
function {viewname}:ctor()\n\
\tself.super.ctor(self , '{viewname}')\n\
\n\
\tself._root = nil  	--ui
\tself:initGraph()\n\
\tself:initData()\n\
end\n\
\n\n\
function {viewname}:initData()\n\
\tself.super.initGraph(self)\n\
\n\
end\n\
\n\n\
function {viewname}:initGraph()\n\
\tself.super.initGraph(self)\n\
\n\
end\n\
\n\n\
----------------------------------------------------------------------------\n\
----------------------------------------------------------------------------\n\
return {viewname}\n
'''

def crt_view(viewname, parentname="stage.viewbase", filetype=".lua"):
	logging.info("Create the script -->{name}{type}".format(name=viewname, type=filetype))
	ret = TEMP_LATE.replace("{viewname}", viewname, 10)
	ret = ret.replace("{parentname}", "'" + parentname + "'")

	path_script = os.path.join(os.getcwd(), (viewname + filetype).lower())
	
	if os.path.isfile(path_script):
		os.remove(path_script)
	
	obj_file = open(path_script, 'w+')
	obj_file.write(ret)
	obj_file.close()
	return ret

def crt_win(viewname, parentname = "stage.winbase" , filetype = ".lua"):
	logging.info("Create the script -->{name}{type}".format(name=viewname, type=filetype))

	ret = TEMP_LATE.replace("{viewname}", viewname, 100)
	ret = ret.replace("{parentname}", "'" + parentname + "'")

	path_script = os.path.join(os.getcwd(), viewname + filetype)
	
	if os.path.isfile(path_script):
		os.remove(path_script)
	
	obj_file = open(path_script, 'w+')
	obj_file.write(ret)
	obj_file.close()
	return ret

def _chk_stagetype(modulename):
	if string.find(modulename.lower(), "win") == 0:
		crt_win(modulename)
	elif string.find(modulename.lower(), "view") == 0:
		crt_view(modulename)

if __name__ == "__main__":
	if len(sys.argv) <= 1:
		logging.error("Enter the view or win name!")
	_chk_stagetype(sys.argv[1])

